package playing.action;

import object.Plant;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import playing.*;

public class friends_menu extends JFrame {
    private int FiD;
    private int id;

    public friends_menu(int FiD, int id) {
        this.FiD = FiD;
        this.id = id;

        setTitle("Friends Menu");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

        // Display all plants of the friend
        JTextArea plantDisplayArea = new JTextArea();
        plantDisplayArea.setBounds(20, 20, 360, 100);
        plantDisplayArea.setEditable(false);
        add(plantDisplayArea);
        shouall(FiD, plantDisplayArea);

        // Buttons for actions
        JButton waterFertilizeButton = new JButton("帮好友浇水施肥");
        waterFertilizeButton.setBounds(20, 140, 160, 30);
        waterFertilizeButton.addActionListener(e -> waterFertilize());
        add(waterFertilizeButton);

        JButton sendPlantButton = new JButton("给好友赠送植物");
        sendPlantButton.setBounds(200, 140, 160, 30);
        sendPlantButton.addActionListener(e -> sendPlant());
        add(sendPlantButton);

        JButton backButton = new JButton("回到主页面");
        backButton.setBounds(20, 180, 340, 30);

        backButton.addActionListener(e -> goToMainMenu());
        add(backButton);

        setVisible(true);
    }

    private void shouall(int id, JTextArea plantDisplayArea) {
        ArrayList<Plant> now_plants = new ArrayList<>();
        List<Integer> now_plants_ids = new PlayerPlantReading().getPlantIdsByUserId(id);
        for (int i = 0; i < now_plants_ids.size(); i++) {
            now_plants.add(new PlantDataReader().getPlantById(now_plants_ids.get(i)));
        }
        if (!now_plants.isEmpty()) {
            StringBuilder displayText = new StringBuilder("他的植物目前有:\n");
            for (int i = 0; i < now_plants.size(); i++) {
                displayText.append(i + 1).append("号植物: ").append(now_plants.get(i).toString()).append("\n");
            }
            plantDisplayArea.setText(displayText.toString());
        } else {
            plantDisplayArea.setText("他现在还没有一个植物");
        }
    }

    private void waterFertilize() {
        String input = JOptionPane.showInputDialog(this, "输入你要帮它的那盆植物浇水施肥的ID:");
        int Pid = Integer.parseInt(input);
        Plant plant = new PlantDataReader().getPlantById(Pid);
        if (plant == null) {
            JOptionPane.showMessageDialog(this, "植物不存在");
            return;
        }

        String[] options = {"浇水", "施肥", "取消"};
        int choice;
        do {
            choice = JOptionPane.showOptionDialog(this, "选择操作:", "浇水施肥",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
                    null, options, options[0]);

            if (choice == 0) { // Water
                plant.setGrowthLevel(plant.getGrowthLevel() + 10);
                JOptionPane.showMessageDialog(this, "浇水成功");
            } else if (choice == 1) { // Fertilize
                plant.setGrowthLevel(plant.getGrowthLevel() + 40);
                JOptionPane.showMessageDialog(this, "施肥成功");
            }
        } while (choice == 0 || choice == 1);

        new PlantDataReader().savePlant(plant);
        new friends_menu(FiD, id);
        dispose();
    }

    private void sendPlant() {
        String input = JOptionPane.showInputDialog(this, "输入你要赠送给他的植物ID:");
        int Pid = Integer.parseInt(input);
        Plant plant = new PlantDataReader().getPlantById(Pid);
        if (plant == null) {
            JOptionPane.showMessageDialog(this, "错误,植物不存在");
            return;
        }

        new PlayerPlantReading().updatePlantForUser(id, Pid, FiD);
        JOptionPane.showMessageDialog(this, "赠送成功");
    }

    private void goToMainMenu() {

        dispose();
    }

    public static void main(String[] args) {
        // For testing purposes
        new friends_menu(1, 1);
    }
}
